//
//  ViewController.m
//  两个label的自动布局
//
//  Created by keisun on 2018/3/20.
//  Copyright © 2018年 keisun. All rights reserved.
//

#import "ViewController.h"
#import "MyView.h"
#import "Masonry.h"
@interface ViewController ()
@property(nonatomic,strong)UIScrollView *scrollView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //两个label自动布局的案例
    [self setView];
    
    //从scrollView了解masonry设置好约束后，完成布局需要一点时间
//    [self setScrollView];
}

-(void)setView{
    MyView *view = [[MyView alloc] initWithFrame:CGRectMake(0, 0, 500, 40)];
    view.backgroundColor = [UIColor blueColor];
    [self.view addSubview:view];
}


//从ScrollView案例中可以发现约束设置后不是立马布局，需要一点时间
-(void)setScrollView{
    UIScrollView *scrolView = [[UIScrollView alloc] init];
    scrolView.backgroundColor = [UIColor redColor];
    [self.view addSubview:scrolView];
    self.scrollView = scrolView;
    [scrolView mas_makeConstraints:^(MASConstraintMaker *make) {
        NSLog(@"建立约束");//先执行block中的，再执行后面的内容。所以约束是先设置的
        make.edges.mas_equalTo(UIEdgeInsetsMake(90, 90, 90, 90));
    }];
    //上面分析了，约束是设置好了，但打印frame发现布局没有立马生效。打印结果：{{0, 0}, {0, 0}}
    NSLog(@"直接获取frame:%@",NSStringFromCGRect(scrolView.frame));
    
    //这里打印发现布局生效了
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"0.1秒后获取frame:%@",NSStringFromCGRect(scrolView.frame));
    });
}

//viewDidLayoutSubviews方法可保证视图布局是生效的，可直接在这里获取视图的frame
-(void)viewDidLayoutSubviews{
    NSLog(@"viewDidLayoutSubviews的frame:%@",NSStringFromCGRect(self.scrollView.frame));
}

@end
